using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Countdown
{
    public partial class SettingsForm : Form
    {
        CountdownForm cform;

        /// <summary>
        /// Creates the new CountdownForm and restores settings.
        /// </summary>
        public SettingsForm()
        {
            InitializeComponent();

            // Create countdown form
            cform = new CountdownForm(this);
            try
            {
                cform.Location = Properties.Settings.Default.Location;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message + "\n" + e.StackTrace);
            }

            // Read settings
            // Changes to the controls will trigger the
            // event handlers automatically.
            eventTextBox.Text = Properties.Settings.Default.EventName;
            opacityTrackBar.Value = Properties.Settings.Default.Opacity;
            topmostCheckBox.Checked = Properties.Settings.Default.TopMost;
            visibleCheckBox.Checked = Properties.Settings.Default.ShowCountdown;
            constrainRightCheckBox.Checked = Properties.Settings.Default.ConstrainRight;

            // Default the date picker to current date rather
            // than the pre-defined minimum date
            try
            {
                countdownDateTimePicker.Value = Properties.Settings.Default.EventDate;
            }
            catch // Date must not be initialized yet
            {
                Properties.Settings.Default.EventDate = DateTime.Now;
                countdownDateTimePicker.Value = DateTime.Now;
            }
        }

        /// <summary>
        /// Utility method to allow the countdown window to
        /// set the tooltip value.
        /// </summary>
        /// <param name="message"></param>
        public void UpdateTooltip(string message)
        {
            countdownNotifyIcon.Text =
                string.Format("Countdown: {0}", message);
        }

        /// <summary>
        /// Utility method to invoke when the countdown is done.
        /// </summary>
        public void EventReached()
        {
            string message = eventTextBox.Text;
            if (message.Length == 0)
            {
                message = "Time's up!";
            }

            countdownNotifyIcon.ShowBalloonTip(5, "Countdown!", message, ToolTipIcon.Info);
        }

        /// <summary>
        /// Event handler for Visible check box.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void visibleCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            cform.Visible = visibleCheckBox.Checked;
            Properties.Settings.Default.ShowCountdown = 
                visibleCheckBox.Checked;
        }

        /// <summary>
        /// Event handler for Topmost check box.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void topmostCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            cform.TopMost = topmostCheckBox.Checked;
            Properties.Settings.Default.TopMost = 
                topmostCheckBox.Checked;
        }

        /// <summary>
        /// Event handler for Opacity track bar.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void opacityTrackBar_ValueChanged(object sender, EventArgs e)
        {
            cform.Opacity = opacityTrackBar.Value * .01;
            Properties.Settings.Default.Opacity = 
                opacityTrackBar.Value;
        }

        /// <summary>
        /// Event handler for the date picker
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void countdownDateTimePicker_ValueChanged(object sender, EventArgs e)
        {
            cform.EventDate = countdownDateTimePicker.Value.Date;
            Properties.Settings.Default.EventDate = 
                countdownDateTimePicker.Value.Date;
        }

        /// <summary>
        /// Event handler for the event name text box.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void eventTextBox_TextChanged(object sender, EventArgs e)
        {
            cform.EventName = eventTextBox.Text;
            Properties.Settings.Default.EventName = eventTextBox.Text;
        }

        /// <summary>
        /// Event handler for Constrain Right check box.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void constrainRightCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            cform.ConstrainToRightSide = constrainRightCheckBox.Checked;
            Properties.Settings.Default.ConstrainRight = 
                constrainRightCheckBox.Checked;
        }

        /// <summary>
        /// Event handler for Close button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void closeButton_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.Save();
            this.Visible = false;
        }

        /// <summary>
        /// Event handler for the notify icon double-click.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void countdownNotifyIcon_DoubleClick(object sender, EventArgs e)
        {
            this.Visible = true;
        }

        /// <summary>
        /// Event handler for the menu Settings option.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Visible = true;
        }

        /// <summary>
        /// Event handler for menu Exit option.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.countdownNotifyIcon.Visible = false;
            Application.Exit();
        }
    }
}